package com.honghu.villagepost.activity

import android.os.Bundle
import com.ada.common.util.CustomHelper
import com.blankj.utilcode.util.ToastUtils
import com.honghu.villagepost.R
import com.honghu.villagepost.api.ApiService
import com.honghu.villagepost.callback.DialogCallback
import com.honghu.villagepost.callback.LzyResponse
import com.honghu.villagepost.entity.FileResult
import com.honghu.villagepost.entity.User
import com.honghu.villagepost.util.GlideUtil
import com.honghu.villagepost.widget.PhotoPopup
import com.honghu.villagepost.widget.SexPopup
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_person.*
import org.devio.takephoto.app.TakePhotoActivity
import org.devio.takephoto.model.TResult
import java.io.File


/**
 * @author: mqwang
 * @create: 2/24/21 10:54 AM
 * @Description:
 */
class PersonActivity : TakePhotoActivity() {
    private var sex: Int = 0
    private var imgsrc: String? = ""
    private var sexPop: BasePopupView? = null
    private var selectPic: BasePopupView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_person)
        common_toolbar.setNavigationOnClickListener {
            finish()
        }

        ApiService.user_info(this, object : DialogCallback<LzyResponse<User>>() {
            override fun onSuccess(response: Response<LzyResponse<User>>?) {
                GlideUtil.loadCircleImg(iv_image, response!!.body().data.avatar)
                et_name.setText(response.body().data.nickname)
                et_phone.setText(response.body().data.mobile)
                sex = response.body().data.sex;
                setSex()
            }
        })

        layout_photo.setOnClickListener {
            selectPic = XPopup.Builder(this)
                    .autoOpenSoftInput(true)
                    .setPopupCallback(object : SimpleCallback() {
                        override fun onDismiss(popupView: BasePopupView?) {
                            super.onDismiss(popupView)
                        }
                    })
                    .asCustom(PhotoPopup(this, object : PhotoPopup.Onclick {
                        override fun first() {
                            CustomHelper.TakePhoto(takePhoto, CustomHelper.PICK_BY_TAKE, false)
                            selectPic?.dismiss()
                        }

                        override fun second() {
                            CustomHelper.TakePhoto(takePhoto, CustomHelper.PICK_BY_SELECT, false)
                            selectPic?.dismiss()
                        }
                    }))
            selectPic?.show()
        }

        layout_sex.setOnClickListener {
            sexPop = XPopup.Builder(this)
                    .autoOpenSoftInput(true)
                    .setPopupCallback(object : SimpleCallback() {
                        override fun onDismiss(popupView: BasePopupView?) {
                            super.onDismiss(popupView)
                        }
                    })
                    .asCustom(SexPopup(this, object : SexPopup.Onclick {
                        override fun first() {
                            sex = 1
                            setSex()
                            sexPop?.dismiss()
                        }

                        override fun second() {
                            sex = 2
                            setSex()
                            sexPop?.dismiss()
                        }
                    }));

            sexPop?.show()

        }

        tv_sub_title.setOnClickListener {
            var name = et_name.text.toString()
            var phone = et_phone.text.toString()
            ApiService.user_modify(this, imgsrc, name, phone, sex, object : DialogCallback<LzyResponse<Any>>() {
                override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                    ToastUtils.showShort(getString(R.string.success))
                    finish()
                }

            })
        }

    }

    fun setSex() {
        if (sex == 0) {
            tv_sex.setText("未知")
        } else if (sex == 1) {
            tv_sex.setText("男")
        } else {
            tv_sex.setText("女")
        }
    }


    override fun takeSuccess(result: TResult?) {
        super.takeSuccess(result)

        ApiService.uploader(this, File(result!!.image.compressPath), object : DialogCallback<LzyResponse<FileResult>>() {
            override fun onSuccess(response: Response<LzyResponse<FileResult>>?) {
                val data = response?.body()?.data
                if (data != null) {
                    imgsrc = data.url
                    GlideUtil.loadCircleImg(iv_image, imgsrc)
                }
            }
        })
    }

}